import React from 'react';
import ToastContainer, { ToastProps } from './ToastContainer';
import { addTopView } from '../TopView';
let isShowing = false;

function notice(toastProps: ToastProps) {
	if (isShowing) return;
	isShowing = true;
	return new Promise(resolve => {
		addTopView(props => (
			<ToastContainer
				{...toastProps}
				onClose={() => {
					isShowing = false;
					props.destroy();
					resolve();
				}}
			/>
		));
	});
}
export default {
	SHORT: 3,
	LONG: 8,
	show(content: string, duration?: number) {
		return notice({ content, type: 'info', duration });
	},

	success(content: string, duration?: number) {
		return notice({ content, type: 'success', duration });
	},
	fail(content: string, duration?: number) {
		return notice({ content, type: 'fail', duration });
	},
	offline(content: string, duration?: number) {
		return notice({ content, type: 'offline', duration });
	},
	loading(content: string, duration?: number) {
		return notice({ content, type: 'loading', duration });
	},
};
